package com.jr.webrtc.controller;

import com.jr.webrtc.model.*;
import com.jr.webrtc.repository.*;
import com.jr.webrtc.config.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/api/users")
public class UserController {

    @Autowired
    private UserRepository userRepository;

    @GetMapping
    public List<User> getAllUsers() {
        return userRepository.findAll();
    }

    @PutMapping("/{id}")
    public ResponseEntity<String> updateUser(@PathVariable Long id, @RequestBody User updatedUser) {
        return userRepository.findById(id)
            .map(user -> {
                user.setUsername(updatedUser.getUsername());
                user.setEmail(updatedUser.getEmail());
                user.setRoomid(updatedUser.getRoomid());
                //密码是第一次设置，后续只能修改其它资料内容。
                //user.setPassword(updatedUser.getPassword());
                userRepository.save(user);
                return ResponseEntity.ok("用户更新成功");
            })
            .orElse(ResponseEntity.notFound().build());
    }
}